﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MinhTam.DataAccess.Web;
using MinhTam.Objects;
using System.Data;
public partial class DMDiaDiemDuLich : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {          
            HelpControls.InitListTinhThanh(ddlTinhThanh);
          
            InitListData();
            txtMoTa.config.toolbar = HelpUtil.GetSimpleCKEditorConfig(this);
            txtMoTa.config.uiColor = HelpUtil.GetCKEditorUiColorAdmin(this);
        }
    }
    
    private void InitListData()
    {
        string sql = @"SELECT ROW_NUMBER() OVER (ORDER BY isnull(tt.sap_xep,9999), tt.ten_tinh_thanh, dd.TEN_DIA_DIEM) STT, tt.TEN_TINH_THANH,dd.*
        FROM DIA_DIEM_DU_LICH dd
        LEFT JOIN TINH_THANH tt ON tt.TINH_THANH_ID=dd.TINH_THANH
        ";
        DataTable dt = SQLConnectWeb.GetTable(sql, DIA_DIEM_DU_LICH.sTableName);
        gridViewData.DataKeyNames = new string[] {
            DIA_DIEM_DU_LICH.cl_DIA_DIEM_ID, 
            DIA_DIEM_DU_LICH.cl_TEN_DIA_DIEM,
            DIA_DIEM_DU_LICH.cl_DIA_CHI, 
            DIA_DIEM_DU_LICH.cl_SO_DIEN_THOAI,      
            DIA_DIEM_DU_LICH.cl_TINH_THANH,
            DIA_DIEM_DU_LICH.cl_MO_TA ,
         DIA_DIEM_DU_LICH.cl_MAP_URL};
        gridViewData.AutoGenerateColumns = false;
        gridViewData.DataSource = dt;
        gridViewData.DataBind();

    }
    protected void gridViewData_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DataKey row = gridViewData.DataKeys[int.Parse(e.CommandArgument.ToString())];
        if (e.CommandName == "cmdEdit")
        {
         //   GridViewRow row = gridViewData.Rows[];
          
            keyField.Value = row.Values[DIA_DIEM_DU_LICH.cl_DIA_DIEM_ID].ToString();
            txtTenVanPhong.Text = row.Values[DIA_DIEM_DU_LICH.cl_TEN_DIA_DIEM].ToString();
            txtDiaChi.Text = row.Values[DIA_DIEM_DU_LICH.cl_DIA_CHI].ToString();
       
            txtSoDienThoai.Text = row.Values[DIA_DIEM_DU_LICH.cl_SO_DIEN_THOAI].ToString();
           
            txtMoTa.Text = row.Values[DIA_DIEM_DU_LICH.cl_MO_TA].ToString();
            ddlTinhThanh.SelectedValue = row.Values[DIA_DIEM_DU_LICH.cl_TINH_THANH].ToString();
            txtURL.Text = row.Values[DIA_DIEM_DU_LICH.cl_MAP_URL].ToString();
            btnSave.Text = "Cập nhật";

        }
        else if (e.CommandName == "cmdDelete")
        {
            string delete = row.Values[DIA_DIEM_DU_LICH.cl_DIA_DIEM_ID].ToString();
           // HelpControls.ShowMsgBoxConfirmDelete(this);
            string sql = string.Format("delete from {0} where {1}={2}", DIA_DIEM_DU_LICH.sTableName, DIA_DIEM_DU_LICH.cl_DIA_DIEM_ID, delete);
            if (SQLConnectWeb.ExecSQL(sql))
            {
              //  HelpControls.ShowMsgBox(this, "Xóa dữ liệu thành công!");
                InitListData();
                if (delete == keyField.Value)
                    btnClearNew_Click(null, null);
            }
            else
            {
                HelpControls.ShowMsgBox(this, "Dữ liệu đang được sử dụng!");
            }
         
        }
    }

    protected void btnClearNew_Click(object sender, EventArgs e)
    {
        txtDiaChi.Text = "";
        txtSoDienThoai.Text = "";
      
        txtTenVanPhong.Text = "";
        keyField.Value = "";
        txtMoTa.Text = "";
        txtURL.Text = "";
        btnSave.Text = "Lưu";
      
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(keyField.Value))//insert
        {
            DIA_DIEM_DU_LICH obj = DIA_DIEM_DU_LICH.Insert_Object(txtTenVanPhong.Text, txtDiaChi.Text, ddlTinhThanh.SelectedValue, txtSoDienThoai.Text, txtMoTa.Text.Replace("'", ""), txtURL.Text.Replace("'", ""));
            if (obj == null)
                HelpControls.ShowMsgBox(this, "Lưu dữ liệu thất bại!");
            else
            {
                InitListData();
                keyField.Value = obj.DIA_DIEM_ID;
                btnSave.Text = "Cập nhật";
            }
            
        }
        else
        {
            DIA_DIEM_DU_LICH dd = new DIA_DIEM_DU_LICH();
            dd.DIA_DIEM_ID = keyField.Value;
            if (dd.Save_Object(txtTenVanPhong.Text, txtDiaChi.Text, ddlTinhThanh.SelectedValue, txtSoDienThoai.Text, txtMoTa.Text.Replace("'", ""), txtURL.Text.Replace("'", "")))
            {
                InitListData();
            }
            else
            {
                HelpControls.ShowMsgBox(this, "Lưu dữ liệu thất bại!");
            }
        }
    }

    protected void gridViewData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)e.Row.Cells[1].Controls[0]).OnClientClick = "return confirm('Bạn có chắc là muốn xóa dữ liệu này?');";
        }
    }
}
